﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace TS_XY_Train_Repaire.ToolSystem
{
    public partial class Tool_System_ToolList_ManaForm : DevExpress.XtraEditors.XtraForm
    {
        public Tool_System_ToolList_ManaForm()
        {
            InitializeComponent();
        }

        private void Locomotive_MainBase_TrainList_ManaForm_Load(object sender, EventArgs e)
        {
            _LoadTreeList();
        }

        private void Locomotive_MainBase_TrainList_ManaForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            TS_Win_Locomotive_MainForm.htActionItem.Remove(this.Name);
        }

        private void bbi_Reload_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }
        #region//--机车分类--//
        private void _LoadTreeList()
        {
            tl_Main.Nodes.Clear();
            tl_Main.Refresh();
            LeverString_SysTreeMainList _ls = new LeverString_SysTreeMainList();
            ArrayList pList = new ArrayList();
            try
            {
                string strSQL = "select * from TS_S_T_P_TrainClass where f_del=0";
                SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
                conn.Open();
                SqlCommand cmd = new SqlCommand(strSQL, conn);
                SqlDataReader read = cmd.ExecuteReader();
                while (read.Read())
                {
                    _ls = new LeverString_SysTreeMainList();
                    _ls.f_id = "C" + read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    pList.Add(_ls);
                }
                read.Close();
                strSQL = "select * from TS_S_T_P_TrainType where f_del=0 and f_tcid in (select f_id from TS_S_T_P_TrainClass where f_del=0)";
                cmd = new SqlCommand(strSQL, conn);
                read = cmd.ExecuteReader();
                while (read.Read())
                {
                    _ls = new LeverString_SysTreeMainList();
                    _ls.f_id = "T" + read["f_id"].ToString();
                    _ls.f_name = read["f_name"].ToString();
                    _ls.f_parent = "C" + read["f_tcid"].ToString();
                    pList.Add(_ls);
                }
                read.Close();
                conn.Close();
                conn.Dispose();
            }
            catch
            {
            }
            tl_Main.DataSource = pList;
        }
        private void tl_Main_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e)
        {
            bbi_TrainClass_Edit.Enabled = false;
            bbi_TrainClass_Del.Enabled = false;
            bbi_TrainType_Add.Enabled = false;
            bbi_TrainType_Edit.Enabled = false;
            bbi_TrainType_Del.Enabled = false;
            bbi_TrainNum_Add.Enabled = false;
            try
            {
                switch (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1))
                {
                    case "C":
                        bbi_TrainClass_Edit.Enabled = true;
                        bbi_TrainClass_Del.Enabled = true;
                        bbi_TrainType_Add.Enabled = true;
                        break;
                    case "T":
                        bbi_TrainType_Edit.Enabled = true;
                        bbi_TrainType_Del.Enabled = true;
                        bbi_TrainNum_Add.Enabled = true;
                        break;
                }
            }
            catch
            {
            }
        }
        private void tl_Main_DoubleClick(object sender, EventArgs e)
        {
            _LoadGrid();
        }

        #endregion
        #region//--机车号--//
        private void _LoadGrid()
        {
            try
            {
                string strSQL = "select row_number() over (order by f_name) as f_row,f_id,f_ttid,f_name,f_ttname,f_arbid,f_arsid,(select f_name from TS_S_T_P_RailwayBureau where f_id=TS_S_T_P_Train_List.f_arbid)as f_rbname,(select f_name from TS_S_T_P_RailwayStations where f_id=TS_S_T_P_Train_List.f_arsid)as f_rsname from TS_S_T_P_Train_List  where f_del=0";
                switch (tl_Main.FocusedNode["f_id"].ToString().Substring(0, 1))
                {
                    case "C":
                        strSQL += " and f_ttid in (select f_id from TS_S_T_P_TrainType where f_del=0 and f_tcid=" + tl_Main.FocusedNode["f_id"].ToString().Substring(1) + ")";
                        break;
                    case "T":
                        strSQL += " and f_ttid=" + tl_Main.FocusedNode["f_id"].ToString().Substring(1);
                        break;
                }
                strSQL += " order by f_name";
                DataTable dt = new DataTable();
                TS_Win_Class.FilledDataTable(strSQL, dt);
                gc_Main.DataSource = dt;
            }
            catch
            {
                gc_Main.DataSource = null;
                gc_Main.Refresh();
            }
        }
        private void gc_Main_Click(object sender, EventArgs e)
        {

        }
        #endregion
        #region//--机车种类管理--//
        private void bbi_TrainClass_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass();
            _uc._LoadType = "Add";
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
            }
        }

        private void bbi_TrainClass_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass();
            _uc._LoadType = "Edit";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadName = tl_Main.FocusedNode["f_name"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }

        private void bbi_TrainClass_Del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode.ParentNode["f_id"].ToString().Substring(1);
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainClass();
            _uc._LoadType = "Del";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadName = tl_Main.FocusedNode["f_name"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }
        #endregion

        #region//--机车类型管理--//
        private void bbi_TrainType_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType();
            _uc._LoadType = "Add";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadTCID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadName = tl_Main.FocusedNode["f_name"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }

        private void bbi_TrainType_Edit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType();
            _uc._LoadType = "Edit";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadTCID = tl_Main.FocusedNode.ParentNode["f_id"].ToString().Substring(1);
            _uc._LoadName = tl_Main.FocusedNode["f_name"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }

        private void bbi_TrainType_Del_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string _NowID = tl_Main.FocusedNode.ParentNode["f_id"].ToString().Substring(1);
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainType();
            _uc._LoadType = "Del";
            _uc._LoadID = tl_Main.FocusedNode["f_id"].ToString().Substring(1);
            _uc._LoadTCID = tl_Main.FocusedNode.ParentNode["f_id"].ToString().Substring(1);
            _uc._LoadName = tl_Main.FocusedNode["f_name"].ToString();
            if (_uc.ShowDialog() == DialogResult.OK)
            {
                _LoadTreeList();
                tl_Main.SetFocusedNode(tl_Main.FindNodeByFieldValue("f_id", _NowID));
            }
        }
        #endregion

        #region//--机车号管理--//
        private void bbi_TrainNum_Add_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainNumber _uc = new UserCon.Locomotive.MainBase.TrainList.uc_Locomotive_MainBase_TrainList_TrainNumber();
            _uc.ShowDialog();
            _LoadGrid();
        }
        #endregion


    }
}